package com.sgb.zhaocai.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityPickInvitedApplyDetailsBinding;
import com.sgb.zhaocai.entity.AddInviteExternalListBean;
import com.sgb.zhaocai.viewmodel.PickInvitedApplyDetailsModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;


/**
 * 邀请报名详情页    再次邀请
 **/

public class PickInvitedApplyDetailsActivity extends AppActivity {

    private ActivityPickInvitedApplyDetailsBinding binding;
    private PickInvitedApplyDetailsModel model;
    private String fromWhere;   //1.招标  2.竞价
    private String title;
    private String time;
    private List<String> companyList;
    private String tenderNo;
    private List<String> userList;
    private int invitedState = 0;   //0 邀请报名  1 再次邀请
    private String companyName;
    private String userName;
    private List<String> userNameList = new ArrayList<>();
    private List<String> companyNameList = new ArrayList<>();
    private String type;
    private AddInviteExternalListBean webssitebean;

    public static void start(Activity context, String fromWhere, String title, String time, int invitedState, List<String> companyList, String tenderNo, List<String> userList, List<String> companyNameList, List<String> userNameList,int code, String type) {
        Intent intent = new Intent(context, PickInvitedApplyDetailsActivity.class);
        intent.putExtra("fromWhere", fromWhere);
        intent.putExtra("title", title);
        intent.putExtra("time", time);
        intent.putExtra("type", type);
        intent.putExtra("invitedState", invitedState);
        intent.putStringArrayListExtra("companyList", (ArrayList<String>) companyList);
        intent.putExtra("tenderNo", tenderNo);
        intent.putStringArrayListExtra("userList", (ArrayList<String>) userList);
        intent.putStringArrayListExtra("companyNameList", (ArrayList<String>) companyNameList);
        intent.putStringArrayListExtra("userNameList", (ArrayList<String>) userNameList);
        context.startActivityForResult(intent,code);

    }
    public static void start(Activity context, String fromWhere, String title, String time, int invitedState, List<String> companyList, String tenderNo, List<String> userList, List<String> companyNameList, List<String> userNameList,int code, String type,AddInviteExternalListBean bean) {
        Intent intent = new Intent(context, PickInvitedApplyDetailsActivity.class);
        intent.putExtra("fromWhere", fromWhere);
        intent.putExtra("title", title);
        intent.putExtra("time", time);
        intent.putExtra("type", type);
        intent.putExtra("invitedState", invitedState);
        intent.putStringArrayListExtra("companyList", (ArrayList<String>) companyList);
        intent.putExtra("tenderNo", tenderNo);
        intent.putStringArrayListExtra("userList", (ArrayList<String>) userList);
        intent.putStringArrayListExtra("companyNameList", (ArrayList<String>) companyNameList);
        intent.putStringArrayListExtra("userNameList", (ArrayList<String>) userNameList);
        intent.putExtra("data",(Serializable) bean);
        context.startActivityForResult(intent,code);

    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_invited_apply_details);
        binding.setLifecycleOwner(this);
        model = new ViewModelProvider(this).get(PickInvitedApplyDetailsModel.class);
        companyList = getIntent().getStringArrayListExtra("companyList");
        userList = getIntent().getStringArrayListExtra("userList");
        fromWhere = getIntent().getStringExtra("fromWhere");
        title = getIntent().getStringExtra("title");
        time = getIntent().getStringExtra("time");
        tenderNo = getIntent().getStringExtra("tenderNo");
        invitedState = getIntent().getIntExtra("invitedState", 0);
        companyNameList = getIntent().getStringArrayListExtra("companyNameList");
        userNameList = getIntent().getStringArrayListExtra("userNameList");
        type = getIntent().getStringExtra("type");
        webssitebean = (AddInviteExternalListBean) getIntent().getSerializableExtra("data");
        binding.tvCommit.setEnabled(true);
        initData();
        registerObserver();
    }

    private void registerObserver() {
        model.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(this, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                }
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        model.showToast.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.tvCommit.setEnabled(false);
                MToast.showLongToast1(getActivity(), s, Gravity.CENTER);
                setResult(Activity.RESULT_OK);
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                                   public void run() {
                                       finish();
                                   }
                               },
                        3*1000);
            }
        });
    }

    private void initData() {
        if (invitedState == 0) {
            if ("1".equals(fromWhere)||"3".equals(fromWhere)) {
                binding.includeTitle.titleView.setText("邀请报名");
            } else if ("2".equals(fromWhere)||"4".equals(fromWhere)) {
                binding.includeTitle.titleView.setText("邀请竞价");
            }
            binding.lineInvitedSum.setVisibility(View.VISIBLE);
            if (companyList.size() > 0) {
                binding.invitedSum.setText("共邀请" + companyList.size() + "家企业/个人");
                if (companyNameList.size() == 1) {
                    this.companyName = companyNameList.get(0);
                } else {
                    for (int i = 0; i < companyNameList.size(); i++) {
                        if (i == companyNameList.size() - 1) {
                            this.companyName = this.companyName + companyNameList.get(i);
                        } else if (i == 0) {
                            this.companyName = companyNameList.get(i) + "、";
                        } else {
                            this.companyName = this.companyName + companyNameList.get(i) + "、";
                        }
                    }
                }
                binding.invitedCompany.setText(companyName);
            }
            if (userList.size() > 0) {
                binding.invitedSum.setText("共邀请" + userList.size() + "家企业/个人");
                if (userNameList.size() == 1) {
                    this.userName = userNameList.get(0);
                } else {
                    for (int i = 0; i < userNameList.size(); i++) {
                        if (i == userNameList.size() - 1) {
                            this.userName = this.userName + userNameList.get(i);
                        } else if (i == 0) {
                            this.userName = userNameList.get(i) + "、";
                        } else {
                            this.userName = this.userName + userNameList.get(i) + "、";
                        }
                    }
                }
                binding.invitedCompany.setText(userName);
            }
        } else if (invitedState == 1) {
            binding.includeTitle.titleView.setText("再次邀请");
            binding.lineInvitedSum.setVisibility(View.GONE);
        }

        if ("1".equals(fromWhere)) {
            binding.invitedExample1.setText(title + "正在火热招标中，您非常符合投标人资格要求，诚邀贵单位/个人前来投标。");
            binding.invitedExample2.setText("我方拟对" + title + "进行招标，按照国家有关规定，本着公开、公平、公正的原则，择优选定中标单位负责。如你方有意向参与投标，请于" + time + "前登录经营帮平台提交报名资料。");
            binding.invitedExample3.setText("我司根据工作需要，经上级部门批准，现对" + title + "进行招标采购，欢迎贵司/个人参加此次招投标活动。");
        } else if ("2".equals(fromWhere)) {
            binding.invitedExample1.setText(title + "正在火热招标中，您非常符合投标人资格要求，诚邀贵单位/个人前来投标");
            binding.invitedExample2.setText("我司根据工作需要，经上级部门批准，现对" + title + "进行招标采购，欢迎贵司/个人参加此次招投标活动。");
            binding.invitedExample3.setText("我方拟对" + title + "进行招标，按照国家有关规定，本着公开、公平、公正的原则，择优选定中标单位负责。如你方有意向参与投标，请登录经营帮平台提交报名资料。");
        }else  if ("3".equals(fromWhere)) {
            binding.invitedExample1.setText(title + "正在火热招标中，您非常符合投标人资格要求，诚邀贵单位/个人前来投标。");
            binding.invitedExample2.setText("我方拟对" + title + "进行招标，按照国家有关规定，本着公开、公平、公正的原则，择优选定中标单位负责。如你方有意向参与投标，请于" + time + "前登录经营帮平台提交报名资料。");
            binding.invitedExample3.setText("我司根据工作需要，经上级部门批准，现对" + title + "进行招标采购，欢迎贵司/个人参加此次招投标活动。");
        } else if ("4".equals(fromWhere)) {
            binding.invitedExample1.setText(title + "正在火热招标中，您非常符合投标人资格要求，诚邀贵单位/个人前来投标");
            binding.invitedExample2.setText("我司根据工作需要，经上级部门批准，现对" + title + "进行招标采购，欢迎贵司/个人参加此次招投标活动。");
            binding.invitedExample3.setText("我方拟对" + title + "进行招标，按照国家有关规定，本着公开、公平、公正的原则，择优选定中标单位负责。如你方有意向参与投标，请登录经营帮平台提交报名资料。");
        }

        binding.invitedExample1.setOnClickListener(this);
        binding.invitedExample2.setOnClickListener(this);
        binding.invitedExample3.setOnClickListener(this);
        binding.tvCancel.setOnClickListener(this);
        binding.tvCommit.setOnClickListener(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.invited_example1) {
            binding.invitedExample1.setBackground(getResources().getDrawable(R.drawable.corner5_ef4033_bg));
            binding.invitedExample1Choose.setVisibility(View.VISIBLE);
            binding.invitedExample2.setBackground(getResources().getDrawable(R.drawable.corner5_ededed_bg));
            binding.invitedExample2Choose.setVisibility(View.GONE);
            binding.invitedExample3.setBackground(getResources().getDrawable(R.drawable.corner5_ededed_bg));
            binding.invitedExample3Choose.setVisibility(View.GONE);
            binding.cfkEdit.setEditContent(binding.invitedExample1.getText().toString());
        } else if (id == R.id.invited_example2) {
            binding.invitedExample1.setBackground(getResources().getDrawable(R.drawable.corner5_ededed_bg));
            binding.invitedExample1Choose.setVisibility(View.GONE);
            binding.invitedExample2.setBackground(getResources().getDrawable(R.drawable.corner5_ef4033_bg));
            binding.invitedExample2Choose.setVisibility(View.VISIBLE);
            binding.invitedExample3.setBackground(getResources().getDrawable(R.drawable.corner5_ededed_bg));
            binding.invitedExample3Choose.setVisibility(View.GONE);
            binding.cfkEdit.setEditContent(binding.invitedExample2.getText().toString());
        } else if (id == R.id.invited_example3) {
            binding.invitedExample1.setBackground(getResources().getDrawable(R.drawable.corner5_ededed_bg));
            binding.invitedExample1Choose.setVisibility(View.GONE);
            binding.invitedExample2.setBackground(getResources().getDrawable(R.drawable.corner5_ededed_bg));
            binding.invitedExample2Choose.setVisibility(View.GONE);
            binding.invitedExample3.setBackground(getResources().getDrawable(R.drawable.corner5_ef4033_bg));
            binding.invitedExample3Choose.setVisibility(View.VISIBLE);
            binding.cfkEdit.setEditContent(binding.invitedExample3.getText().toString());
        } else if (id == R.id.tv_cancel || id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_commit) {
            String inviteConditions = binding.cfkEdit.getEditContent();
            if (invitedState == 0) {
                if ("1".equals(fromWhere)) {
                    model.addBiddingInvite((String[]) companyList.toArray(new String[companyList.size()]), inviteConditions, tenderNo, fromWhere, (String[]) userList.toArray(new String[userList.size()]), type);
                } else if ("2".equals(fromWhere)) {
                    model.addBidInvite((String[]) companyList.toArray(new String[companyList.size()]), inviteConditions, tenderNo, fromWhere, (String[]) userList.toArray(new String[userList.size()]), type);
                } else if ("3".equals(fromWhere)) {
                    //邀请非网站客户信息
                    webssitebean.setInviteConditions(inviteConditions);
                    model.saveZcInviteTenderExternal(webssitebean);
                }else if ("4".equals(fromWhere)) {
                    //竞价非网站客户信息
                    webssitebean.setInviteConditions(inviteConditions);
                    model.saveZcInviteBidExternal(webssitebean);
                }
            } else if (invitedState == 1) {
                if ("1".equals(fromWhere)) {
                    model.inviteBiddingAgain(tenderNo);
                } else if ("2".equals(fromWhere)) {
                    model.inviteBidAgain(tenderNo);
                }else if ("3".equals(fromWhere)) {
                    //再次邀请非网站客户信息
                    webssitebean.setInviteConditions(inviteConditions);
                    model.saveZcInviteTenderExternal(webssitebean);
                }else if ("4".equals(fromWhere)) {
                    //再次竞价非网站客户信息
                    webssitebean.setInviteConditions(inviteConditions);
                    model.saveZcInviteBidExternal(webssitebean);
                }
            }
        }
    }
}